﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;
using VisionRoc.Tool;

namespace VisionRoc_Measure
{
    public partial class FormCircle1 : Form
    {
        HObject m_Image;
        SetGenCircle m_Circle;
        public FormCircle1(SetGenCircle _Circle, HObject _Image)
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景 
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲  
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.DoubleBuffered = true;//避免时间显示控件闪烁
            InitializeComponent();
            m_Circle = _Circle;
            m_Image = _Image;
        }

        private void FormCircle1_Load(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.SetDraw(this.hWindowControl1.HalconWindow, "margin");
                HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
                HOperatorSet.SetColored(this.hWindowControl1.HalconWindow, 12);
                HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);

                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text += "图像为空，请加载图像！";
                }
                else
                {
                    HOperatorSet.GetImageSize(m_Image, out HTuple Wideth, out HTuple Height);
                    HOperatorSet.SetSystem("width", Wideth);
                    HOperatorSet.SetSystem("height", Height);
                    MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Wideth, Height);
                    HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                }

                if (m_Circle != null)
                {

                    m_Circle.ReadParameter();
                    this.propertyGrid1.SelectedObject = m_Circle;
                    this.propertyGrid1.Refresh();
                }
                else
                {
                    tool_ResultInfo.Text += "圆区域 工具模块对象不存在。";
                }

                string result = "";
                Global.m_MultiLanguage.LControls = new List<Control>();
                Global.m_MultiLanguage.GetControls2(this);
                if (!Global.m_MultiLanguage.LoadLanguage(this, out result))
                {
                    MessageBox.Show(result + "\r\n程序当前语言可能异常，如果影响正常使用请重新启动程序", "加载语言", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btn_DrawRectangle1_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "图像为空！";
                    MessageBox.Show("图像为空，请加载图像！");
                    return;
                }
                btn_DrawRectangle1.Enabled = false;
                btn_Clear.Enabled = false;
                btn_DisplayRegion.Enabled = false;
                btn_Refresh.Enabled = false;
                btn_SaveParameter.Enabled = false;
                HOperatorSet.SetColored(this.hWindowControl1.HalconWindow, 12);
                HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                hWindowControl1.Focus();
                MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "左键绘制，右键结束", "window", 20, 20, 15, "green");
                HTuple Row = new HTuple(); HTuple Col = new HTuple(); HTuple Radius = new HTuple();
                Row.Dispose(); Col.Dispose(); Radius.Dispose();
                HOperatorSet.DrawCircle(hWindowControl1.HalconWindow, out Row, out Col, out Radius);
                if (Row.D > 0)
                {
                    m_Circle.Row1 = Row.D; m_Circle.Col1 = Col.D; m_Circle.Radius = Radius.D;
                    HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                    HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                    MyHOperatorSet.DispMeassge(hWindowControl1.HalconWindow, "绘制成功", "window", 20, 20, 15, "green");
                    tool_ResultInfo.Text = $"绘制圆区域成功。【Row1：{Math.Round(m_Circle.Row1, 2)}】。【Col1：{Math.Round(m_Circle.Col1, 2)}】。【Radius：{Math.Round(m_Circle.Radius, 2)}】。";
                    btn_DrawRectangle1.Enabled = true;
                    btn_Clear.Enabled = true;
                    btn_DisplayRegion.Enabled = true;
                    btn_Refresh.Enabled = true;
                    btn_SaveParameter.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
            HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
            m_Circle.Row1 = 0; m_Circle.Col1 = 0; m_Circle.Radius = 0;
            if (m_Circle.Row1 == 0 && m_Circle.Col1 == 0 && m_Circle.Radius == 0)
            {
                tool_ResultInfo.Text = "清除区域成功。";
            }
        }

        private void btn_DisplayRegion_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.CountObj(m_Image, out HTuple tuple1);
                if (tuple1.I == 0)
                {
                    tool_ResultInfo.Text = "图像为空！";
                    MessageBox.Show("图像为空，请加载图像！");
                    return;
                }
                if (m_Circle.Row1 > 0 && m_Circle.Col1 > 0 && m_Circle.Radius > 0)
                {
                    HObject Circle;
                    HOperatorSet.GenEmptyObj(out Circle);
                    Circle.Dispose();
                    HOperatorSet.GenCircle(out Circle, m_Circle.Row1, m_Circle.Col1, m_Circle.Radius);
                    HOperatorSet.DispObj(Circle, hWindowControl1.HalconWindow);
                }
                else
                {
                    tool_ResultInfo.Text = "圆型区域 参数为空，请绘制矩形。";
                    MessageBox.Show("圆型区域 参数为空，请绘制矩形。");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.ClearWindow(hWindowControl1.HalconWindow);
                HOperatorSet.SetColor(this.hWindowControl1.HalconWindow, "red");
                HOperatorSet.SetLineWidth(this.hWindowControl1.HalconWindow, 2);
                HOperatorSet.DispObj(m_Image, hWindowControl1.HalconWindow);
                if (m_Circle != null)
                {
                    m_Circle.ReadParameter();
                    this.propertyGrid1.SelectedObject = m_Circle;
                    this.propertyGrid1.Refresh();
                    tool_ResultInfo.Text = "刷新成功。";
                }
                //MessageBox.Show("刷新成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btn_SaveParameter_Click(object sender, EventArgs e)
        {

            try
            {
                m_Circle.WriteParameter();
                MessageBox.Show("保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
